/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufsc.aps.convocados;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

/**
 *
 * @author gustavo.ramos
 */
public class DialogTrocarEsquema extends javax.swing.JDialog {

    /**
     * Creates new form DialogTrocarEsquema
     */
    public DialogTrocarEsquema(PanelTabuleiro parent, boolean modal, List<Jogador> jogadores) {
        super(parent.getParentFrame(), modal);
        initComponents();
        this.montarLabels(jogadores, parent);
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        this.setLocationRelativeTo(parent);
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelBase = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        panelBase.setLayout(new java.awt.GridLayout(2, 5, 1, 1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelBase, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelBase, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void montarLabels(List<Jogador> jogadores, PanelTabuleiro parent) {
        for (Jogador j : jogadores) {
            LabelJogador lj = new LabelJogador(j, true);
            lj.addMouseListener(montarMouseListener(parent, lj));
            panelBase.add(lj);
        }
    }

    private void verificarSePanelBaseTaVazio() {
        if (panelBase.getComponents().length == 0) {
            this.dispose();
        }
    }
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel panelBase;
    // End of variables declaration//GEN-END:variables

    private MouseListener montarMouseListener(final PanelTabuleiro parent, final LabelJogador lj) {
        return new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (parent.adicionarJogador(true, lj.getJogador(), false)) {
                    panelBase.remove(lj);
                    parent.montar();
                    verificarSePanelBaseTaVazio();
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        };
    }
}
